package com.lovo.movie.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
    //创建了一个队列
    @Bean
    public Queue movieQueue(){
        return new Queue("movieQueue");
    }
    @Bean
     public Queue queueOne(){
        return new Queue("queueOne");
     }
    @Bean
    public Queue queueTwo(){
        return new Queue("queueTwo");
    }
    @Bean
    public Queue queueThree(){
        return new Queue("queueThree");
    }
    @Bean
    public Queue queueFour(){
        return new Queue("queueFour");
    }
//    @Bean
//    public Queue orderQueue(){
//        return new Queue("orderQueue");
//    }




   //创建了一个交换机
    @Bean
    public DirectExchange movieExchange(){
        return new DirectExchange("movieExchange");
    }
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }
    @Bean
    public  FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }
     //把队列与交换机进行绑定
    @Bean
    public Binding directExchangeToMovieQueue(DirectExchange movieExchange,Queue movieQueue){
        return BindingBuilder.bind(movieQueue).to(movieExchange).with("movie.basic");
    }
    @Bean
    public Binding topicExchangeToQueueOne(TopicExchange topicExchange,Queue queueOne){
         return BindingBuilder.bind(queueOne).to(topicExchange).with("test.#");
    }
    @Bean
    public Binding topicExchangeToQueueTwo(TopicExchange topicExchange,Queue queueTwo){
        return BindingBuilder.bind(queueTwo).to(topicExchange).with("order.#");
    }
    @Bean
    public Binding fanoutExchangeToQueueThree(FanoutExchange fanoutExchange,Queue queueThree){
         return BindingBuilder.bind(queueThree).to(fanoutExchange);
    }
    @Bean
    public Binding fanoutExchangeToQueueFour(FanoutExchange fanoutExchange,Queue queueFour){
        return BindingBuilder.bind(queueFour).to(fanoutExchange);
    }
//    @Bean
//    public Binding fanoutExchangeToOrderQueue(FanoutExchange fanoutExchange,Queue orderQueue){
//        return BindingBuilder.bind(orderQueue).to(fanoutExchange);
//    }
}
